package syn;

public class UnsafeBank {
    public static void main(String[] args) {
        Account account=new Account("结婚基金",100);
        Drawing you =new Drawing(account,50,"you");
        Drawing wife =new Drawing(account,80,"wife");
        you.start();
        wife.start();
    }
}
class Account{
    String name;
    int money;
    public Account(String name, int money) {
        this.name = name;
        this.money = money;
    }
}
class Drawing extends Thread{
    // account
    private Account account;
    // 取多少钱
    private int drawMoney;
    // 手里的钱
    private int nowMoney;
    public Drawing(Account account, int drawMoney, String name) {
        super(name);
        this.account = account;
        this.drawMoney = drawMoney;
        //this.nowMoney = nowMoney;
    }

    @Override
    public void run() {
        synchronized (account) {
            // judge money
            if (account.money - drawMoney < 0) {
                System.out.println(Thread.currentThread().getName() + "钱不够，取不了");
                System.out.println("-----------------");
                return;
            }
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            // draw
            account.money = account.money - drawMoney;
            nowMoney = drawMoney + nowMoney;
            System.out.println(account.name + "余额为" + account.money);
            System.out.println(this.getName() + "手里有" + nowMoney + "钱");
            System.out.println("-----------------");
        }
    }
}
